unit fRTRW;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ZConnection, Setting, DB, ZAbstractRODataset,
  ZAbstractDataset, ZDataset, JvThread, JvComponentBase, JvThreadDialog;

type
  TfrmRTRW = class(TForm)
    Button1: TButton;
    Button2: TButton;
    txtRT: TEdit;
    txtRW: TEdit;
    lblRT: TLabel;
    lblRW: TLabel;
    lblPemilih: TLabel;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
    JvThreadSimpleDialog2: TJvThreadSimpleDialog;
    JvThread2: TJvThread;
    txtAlamat: TEdit;
    Label1: TLabel;
    txtDusun: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    procedure FormCreate(Sender: TObject);
    Function cekRTRW(kata:String) : String;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure txtRTKeyPress(Sender: TObject; var Key: Char);
    procedure txtRWKeyPress(Sender: TObject; var Key: Char);
    procedure KeyOnlyNum(Sender: TObject; var Key: Char);
    procedure Button2Click(Sender: TObject);
    procedure JvThread2Execute(Sender: TObject; Params: Pointer);
    function HandleSpec(Asal: String):String;
    procedure JvThread2FinishAll(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent; tabel: String; ID:String; isganda:Boolean; IDT:String; bta:Integer;  pemil_list:TStringList; user:string=''; tabelx: String=''; IDX:String=''; IDTX:String='');             overload;
  end;

var
  frmRTRW: TfrmRTRW;
  ptabel, tid,pid, idt, ptabelx,pidx, tidx, username, idtgl, rt, rw, almt, dusun : string;
  pemilihs: tstringlist;
  ganda: boolean;
  atb:integer;

implementation

{$R *.dfm}

constructor TfrmRTRW.Create(AOwner: TComponent; tabel: String; ID:String; isganda:Boolean; IDT:String; bta:Integer; pemil_list:TStringList; user:string=''; tabelx: String=''; IDX:String=''; IDTX:String='');
begin
    ptabel:=tabel;
    pid:=ID;
    tid:=IDT;
    ganda:=isganda;
    atb:=bta;
    ptabelx:=tabelx;
    pidx:=IDX;
    tidx:=IDTX;
    username:=user;
    pemilihs:=pemil_list;
//    ShowMessage(ptabel+'-'+pid);
    inherited Create(AOwner);
end;

procedure TfrmRTRW.FormCreate(Sender: TObject);
var
  kabkot,kec,desa,nama,itabel,nik:String;
  i,idx:Integer;
  setting:TSetting_Form;
begin
  setting:=TSetting_Form.Create;
  ZConnection1.HostName:=setting.host;
  ZConnection1.Protocol:=setting.protocol;
  ZConnection1.Database:=setting.database;
  ZConnection1.User:=setting.user;
  ZConnection1.Password:=setting.password;
  ZConnection1.Connected:=true;

  BorderIcons := BorderIcons - [biMaximize];

  //set brp label pemilih nya
  lblPemilih.Caption:='Mengubah '+inttostr(pemilihs.Count)+' pemilih';
end;

Function tfrmRTRW.cekRTRW(kata:String) : String;
var
  stemp:String;
begin
  sTemp:='';
  if (Length(kata)=1) then
    sTemp:='00'+kata
  else if (Length(kata)=2) then
    sTemp:='0'+kata
  else if (Length(kata)>=3) then
    sTemp:=Copy(kata, Length(kata)-3, 3)
  else
    sTemp:='000';
  Result := sTemp;
end;

procedure TfrmRTRW.Button1Click(Sender: TObject);
begin
  Close;
end;

procedure TfrmRTRW.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfrmRTRW.txtRTKeyPress(Sender: TObject; var Key: Char);
begin
  KeyOnlyNum(Sender, Key);
end;

procedure TfrmRTRW.txtRWKeyPress(Sender: TObject; var Key: Char);
begin
  KeyOnlyNum(Sender, Key);
end;

procedure TfrmRTRW.KeyOnlyNum(Sender: TObject; var Key: Char);
begin
  if not(Key in [#8, '0' .. '9']) then
  begin
    Key := #0;
  end;
end;

procedure TfrmRTRW.Button2Click(Sender: TObject);
var
  tanggal, query:string;
begin
  rt := cekRTRW(txtRT.Text);
  rw := cekRTRW(txtRW.Text);
  almt := txtAlamat.Text;
  dusun := txtDusun.Text;

  //masukin tanggal dulu gan
  tanggal := FormatDateTime('yyyy-mm-dd hh:mm:ss', Now());
  with ZQuery1 do  //masukkin nih gan
  begin
    Active:=False;
    sql.Clear;
    sql.Text := 'INSERT INTO tanggal (TANGGAL) VALUES ("'+tanggal+'")';
    ExecSQL;
  end;
  with ZQuery1 do  //masukkin nih gan
  begin
    Active:=False;
    sql.Clear;
    sql.Text := 'SELECT * FROM tanggal ORDER BY ID_TANGGAL DESC';
    Active:=True;
    idtgl := FieldByName('ID_TANGGAL').AsString;  //dapat ID_TGL
  end;

  if((rt <> '000') or (rw <>'000') or (almt <> '') or (dusun <> '')) then begin
    JvThread2.ThreadDialog:=JvThreadSimpleDialog2;
    JvThread2.Execute(Self);
  end
  else
    showMessage('Salah satu field harus diisi');

end;

procedure TfrmRTRW.JvThread2Execute(Sender: TObject; Params: Pointer);
var
  i:integer;
  query,itabel,ique,pidn,tidn,idx,tidx,pidx,ptabelx:String;
  temp:TStrings;
  ket : string;
begin
  //ini for
    for i:=0 to pemilihs.Count-1 do begin
      pid := pemilihs[i];
      //showmessage(pemilihs[i]);
      with Zquery1 do begin
        active:=false;
        sql.Clear;
        query:='SELECT * FROM '+ptabel+' WHERE ID_USER='+pid;
//      ShowMessage(query);
        sql.Text:=query;
        Active:=true;
        first;

        iQue:='INSERT INTO `history` (ID_USER, `NIK`, `NKK`, `NAMA`, `TEMPAT`, `TANGGAL`, '+
        '`IBU`, `AGAMA`, `KAWIN`, `KELAMIN`, `KELAINAN`, `ALAMAT`, `RW`, `RT`, `DUSUN`, `KET`, '+
        '`ID_TANGGAL`, TPS, STATUS1, STATUS2, GANDA, STATUS3, STATUSP, ALASANP'+
        ',CACAT,PENDIDIKAN,PEKERJAAN,STATUSR,WN,USERNAME, KET_AKSI) VALUES '+
        '('+QuotedStr(tid+'-'+FieldByName('ID_USER').AsString)+
        ','+QuotedStr(HandleSpec(FieldByName('NIK').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('NKK').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('NAMA').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('TEMPAT').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('TANGGAL').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('IBU').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('AGAMA').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('KAWIN').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('KELAMIN').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('KELAINAN').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('ALAMAT').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('RW').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('RT').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('DUSUN').AsString))+
        ','+QuotedStr(ket)+
        ','+FieldByName('ID_TANGGAL').AsString+
        ','+FieldByName('TPS').AsString+
        ','+QuotedStr(FieldByName('STATUS1').AsString)+
        ','+QuotedStr(FieldByName('STATUS2').AsString)+
        ','+QuotedStr(FieldByName('GANDA').AsString)+
        ','+QuotedStr(FieldByName('STATUS3').AsString)+
        ','+QuotedStr(FieldByName('STATUSP').AsString)+
        ','+QuotedStr(FieldByName('ALASANP').AsString)+
        ','+QuotedStr(FieldByName('CACAT').AsString)+
        ','+QuotedStr(FieldByName('PENDIDIKAN').AsString)+
        ','+QuotedStr(FieldByName('PEKERJAAN').AsString)+
        ','+QuotedStr(FieldByName('STATUSR').AsString)+
        ','+QuotedStr(FieldByName('WN').AsString)+
        ','+QuotedStr(username)+
        ','+QuotedStr('0')+
        ')';
        //
        active:=false;
        sql.Clear;
        //ShowMessage(ique);
        sql.Text:=ique;
        ExecSQL;

        //update alamat
        if(almt <> '') then begin
          active:=false;
          sql.Clear;
          sql.Text:='UPDATE '+ptabel+' SET ALAMAT = '+quotedStr(almt)+', ID_TANGGAL = '+idtgl+' WHERE ID_USER = '+pid;
          execSQL;
        end;

        //update RT
        if(rt <> '000') then begin
          active:=false;
          sql.Clear;
          sql.Text:='UPDATE '+ptabel+' SET RT = '+quotedStr(rt)+', ID_TANGGAL = '+idtgl+' WHERE ID_USER = '+pid;
          execSQL;
        end;

        //update RW
        if(rw <> '000') then begin
          active:=false;
          sql.Clear;
          sql.Text:='UPDATE '+ptabel+' SET RW = '+quotedStr(rw)+', ID_TANGGAL = '+idtgl+' WHERE ID_USER = '+pid;
          execSQL;
        end;

        //update Dusun
        if(dusun <> '') then begin
          active:=false;
          sql.Clear;
          sql.Text:='UPDATE '+ptabel+' SET DUSUN = '+quotedStr(dusun)+', ID_TANGGAL = '+idtgl+' WHERE ID_USER = '+pid;
          execSQL;
        end;
      end;
    end;
end;

function TFrmRTRW.HandleSpec(Asal: String):String;
var
  temp:string;
begin
  temp := StringReplace(asal, '''', '',[rfReplaceAll, rfIgnoreCase]);
  temp := StringReplace(temp, '\', '',[rfReplaceAll, rfIgnoreCase]);
  result := temp;
end;

procedure TfrmRTRW.JvThread2FinishAll(Sender: TObject);
begin
  Showmessage('data berhasil di-update');
  Close;
end;

end.
